<?php
namespace App\Models\DyModels;
use App\Models\ParentModel;
use DB;
/**
 * 订阅事件的model
 * @author puppet
 */
class ReceiveMsg extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_dy_receive_msg';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_dy_receive_msg';

  /**
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;



	//获取可用的已经授权用户 token 信息
	function userTokenList(){
		$sql="select open_id from pt_dy_access_token where status=?";
		$re=DB::select($sql,[1]);
		return isset($re[0]['open_id']) ? $re : false;
	}

	//获取可用的已经授权用户 token 信息
	function userToken($open_id){
		$sql="select access_token,open_id,refresh_token from pt_dy_access_token where status=? and open_id='{$open_id}'";
		$re=DB::select($sql,[1]);
		return isset($re[0]['open_id']) ? $re[0] : false;
	}

	//获取可用的user授权用户
	function userTokenListTrue(){
		$sql="select a.open_id from pt_user as a left join pt_dy_access_token as b on b.open_id=a.open_id where a.status=? and type=1 and b.status=1";
		$re=DB::select($sql,[1]);
		return isset($re[0]['open_id']) ? $re : false;
	}

	//存入用户 open_id 相关联的粉丝数据
	function upDyUserFans($arr,$p_open_id){
		foreach($arr as $k=>$v){
			$open_id=isset($v['open_id']) ? $v['open_id'] : '';
			$re=DB::select("select id from pt_fans where p_open_id='{$p_open_id}' and open_id=?",[$open_id]);
			$re=isset($re[0]['id']) ? $re[0]['id'] : false;
			$data=[
				'avatar'=>isset($v['avatar']) ? $v['avatar'] : '',
				'city'=>isset($v['city']) ? $v['city'] : '',
				'country'=>isset($v['country']) ? $v['country'] : '',
				'gender'=>isset($v['gender']) ? ($v['gender']==0 ? '未知' : ($v['gender']==1 ? '男' : '女')) : '',
				'nickname'=>isset($v['nickname']) ? htmlspecialchars($v['nickname'],ENT_QUOTES) : '',
				'province'=>isset($v['province']) ? $v['province'] : '',
				'union_id'=>isset($v['union_id']) ? $v['union_id'] : '',
				'update_time'=>date('Y-m-d H:i:s',time()),
				'is_follower'=>1,
			];
			if($re){
				DB::table('pt_fans')->where('open_id',$open_id)->where('p_open_id',$p_open_id)->update($data);
			}else{
				$data['open_id']=$open_id;
				$data['p_open_id']=$p_open_id;
				$data['create_time']=date('Y-m-d H:i:s',time());
				DB::table('pt_fans')->insert($data);
			}
		}
		DB::update("update pt_fans set `is_follower`=0,`update_time`='".date('Y-m-d')."' where p_open_id='{$p_open_id}' and update_time<'".date('Y-m-d')."'");
	}

	//存入用户 open_id 相关联的关注数据
	function upDyUserFollowing($arr,$p_open_id){
		foreach($arr as $k=>$v){
			$open_id=isset($v['open_id']) ? $v['open_id'] : '';
			$re=DB::select("select id from pt_following where p_open_id='{$p_open_id}' and open_id=?",[$open_id]);
			$re=isset($re[0]['id']) ? $re[0]['id'] : false;
			$data=[
				'avatar'=>isset($v['avatar']) ? $v['avatar'] : '',
				'city'=>isset($v['city']) ? $v['city'] : '',
				'country'=>isset($v['country']) ? $v['country'] : '',
				'gender'=>isset($v['gender']) ? ($v['gender']==0 ? '未知' : ($v['gender']==1 ? '男' : '女')) : '',
				'nickname'=>isset($v['nickname']) ? htmlspecialchars($v['nickname'],ENT_QUOTES) : '',
				'province'=>isset($v['province']) ? $v['province'] : '',
				'union_id'=>isset($v['union_id']) ? $v['union_id'] : '',
				'update_time'=>date('Y-m-d H:i:s',time()),
				'is_follower'=>1,
			];
			if($re){
				DB::table('pt_following')->where('open_id',$open_id)->where('p_open_id',$p_open_id)->update($data);
			}else{
				$data['open_id']=$open_id;
				$data['p_open_id']=$p_open_id;
				$data['create_time']=date('Y-m-d H:i:s',time());
				DB::table('pt_following')->insert($data);
			}
		}
		DB::update("update pt_following set `is_follower`=0,`update_time`='".date('Y-m-d')."' where p_open_id='{$p_open_id}' and update_time<'".date('Y-m-d')."'");
	}

	//存入用户 open_id 相关的意向客户
	function upDyUserEnterprise($arr,$p_open_id){
		foreach($arr as $k=>$v){
			$leads_level=isset($v['leads_level']) ? $v['leads_level'] : -1;
			$open_id=isset($v['open_id']) ? $v['open_id'] : '';
			$re=DB::select("select id from pt_enterprise where p_open_id='{$p_open_id}' and open_id=?",[$open_id]);
			$re=isset($re[0]['id']) ? $re[0]['id'] : false;
			$data=[
				'gender'=>isset($v['gender']) ? ($v['gender']==0 ? '未知' : ($v['gender']==1 ? '男' : '女')) : '',
				'leads_level'=>$leads_level,
				'telephone'=>isset($v['telephone']) ? $v['telephone'] : '',
				'avatar'=>isset($v['avatar']) ? $v['avatar'] : '',
				'city'=>isset($v['city']) ? $v['city'] : '',
//				'is_follow'=>isset($v['is_follow'])  ? 1 : 0,
				'nickname'=>isset($v['nickname']) ? htmlspecialchars($v['nickname'],ENT_QUOTES) : '',
				'wechat'=>isset($v['wechat']) ? $v['wechat'] : '',
				'is_follow'=>isset($v['is_follow']) ? $v['age'] : 0,
				'update_time'=>date('Y-m-d H:i:s',time()),
			];
			if($re){
				DB::table('pt_enterprise')->where('open_id',$open_id)->where('p_open_id',$p_open_id)->update($data);
			}else{
				$data['open_id']=$open_id;
				$data['p_open_id']=$p_open_id;
				$data['create_time']=date('Y-m-d H:i:s',time());
				if($leads_level==1 || $leads_level==2){
					DB::table('pt_enterprise')->insert($data);
				}
			}
		}
	}
}